﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using TWS.Controller.Paging;


public partial class ListItem : BaseAdminPage
{
    public int id;
    public string loaisanpham;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            id = Convert.ToInt32(Request.QueryString["id"]);
            loaisanpham = Request.QueryString["type"];
            MasterPageDetail m = this.Master as MasterPageDetail;
            m.loaisanphamid = id;
            if (!new LoaiSanPhamHelper().IsLoaiSanPhamCha(id))
            {
                LoadSanPham();
            }
            else
            {
                TongSanPham.Visible = false;
            }
            LoaiSanPhamDataSet lspDS = new LoaiSanPhamHelper().LoadLoaiSanPham(id);
            
                LoadLoaiSanPham();
            
        }
    }
    private void LoadLoaiSanPham()
    {
        repLoaiSanPham.DataSource = new LoaiSanPhamHelper().LoadLoaiSanPhamCon(id);
        repLoaiSanPham.DataBind();
    }
    protected void repLoaiSanPham_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            LoaiSanPhamHelper lspHP = new LoaiSanPhamHelper();
            Repeater repCon = ((Repeater)e.Item.FindControl("repLoaiSanPhamCon"));
            repCon.DataSource = (lspHP.LoadLoaiSanPhamCon(Convert.ToInt32((((DataRowView)e.Item.DataItem)["LoaiSanPhamID"]))));
            repCon.DataBind();
        }
    }
    private void LoadSanPham()
    {
        string tmpPage = Request.QueryString["pg"];
        int currentPage = 1;
        if (tmpPage != null && tmpPage != "")
        {
            currentPage = Convert.ToInt32(tmpPage);
        }
        SanPhamDataSet spDs = new SanPhamHelper().LoadSanPhamTheoLoai(currentPage, PagingSize, "NgayDang", "DESC", id);

        repSanPham.DataSource = spDs.SanPham;
        repSanPham.DataBind();

        TotalRecord = new SanPhamHelper().TongSanPhamTheoLoai(id);

        ResultCount.Text = TotalRecord.ToString();

        Paging page = getPagingObject(TotalRecord, currentPage, PagingSize);

        PageString = page.BuildPaging();
    }

    

    

    public string GetLoaiSanPhamName()
    {
        LoaiSanPhamHelper lspHP = new LoaiSanPhamHelper();
        LoaiSanPhamDataSet lspDs = lspHP.LoadLoaiSanPham(id);
        LoaiSanPhamDataSet.LoaiSanPhamRow lsp = lspDs.LoaiSanPham[0];
        if (lsp.IsLoaiSanPhamChaIDNull())
        {
            return lsp.TenLoaiSanPham;
        }
        LoaiSanPhamDataSet lspDsCha = lspHP.LoadLoaiSanPham(lsp.LoaiSanPhamChaID);
        LoaiSanPhamDataSet.LoaiSanPhamRow lspCha = lspDsCha.LoaiSanPham[0];

        return lspCha.TenLoaiSanPham + "  >>  " + lsp.TenLoaiSanPham;
    }

    public string LoadBannerQuangCao()
    {
        BannerQuangCaoDataSet bnDS = new BannerQuangCaoHelper().LoadBannerTheoLoai(id);
        if (bnDS.BannerQuangCao.Rows.Count>0)
        {
            return bnDS.BannerQuangCao[0].HinhAnh;
        }
        return "";
    }
}
